package bit2;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

public class bitMain19 {

    static class Worker extends Thread{
        private BlockingQueue<Runnable> queue=null;
        public Worker(BlockingQueue<Runnable> queue){
            this.queue=queue;
        }
        @Override
        public void run() {
            while (true){
                try {
                    Runnable command=queue.take();
                    command.run();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    static class ThreadPool{
        private BlockingDeque<Runnable> queue=new LinkedBlockingDeque<>();
        private List<Thread> workers=new ArrayList<>();
        private static final int MAX_WORKER_COUNT=10;
        public void submit(Runnable commmand) throws InterruptedException {
            if (workers.size()<MAX_WORKER_COUNT){
                Worker worker=new Worker(queue);
                worker.start();
                workers.add(worker);
            }
            queue.put(commmand);
        }
    }
    public static void main(String[] args) throws InterruptedException {
        ThreadPool pool=new ThreadPool();
        for (int i = 0; i < 20; i++) {
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello");
                }
            });
        }
    }
}
